/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlykho;

import java.awt.Color;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author Admin
 */
public class QuanLyNV extends javax.swing.JPanel implements TreeSelectionListener{

    /**
     * Creates new form QuanLyNV
     */
    static DefaultMutableTreeNode root ;
   static DefaultTreeModel model;
    static DefaultTreeModel modelTree;
    public QuanLyNV() {
        try {
            initComponents();
                        Ketnoi a = new Ketnoi();
                Connection con = a.connect();
                root = new DefaultMutableTreeNode("Quản lý nhân viên");
                model = new   DefaultTreeModel(root) ;
                 modelTree = (DefaultTreeModel)tree.getModel();
                 modelTree.setRoot(root);
                PreparedStatement pst = con.prepareCall("Select * from bophan");
                ResultSet rs = pst.executeQuery();
                String str = null;
                String ten = null;
                while (rs.next())
            {
                str = rs.getString(2);
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(str);
                ten =rs.getString(1);
                PreparedStatement pst1 = con.prepareCall("Select * from nhanvien where mabp = ? ");
                pst1.setString(1, ten);     
                ResultSet rs1 = pst1.executeQuery();
                String str1 = null;
                while(rs1.next())
                {
                 str1=rs1.getString(2);
                 DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(str1);
                 //System.out.println(str1);
                 node.add(node1);
                }
               // System.out.println("alo");
                root.add(node);
            }
                tree.addTreeSelectionListener(this);
        } catch (SQLException ex) {
            Logger.getLogger(QuanLyNV.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();
        p1 = new javax.swing.JPanel();

        tree.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tree.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        tree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(tree);

        p1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout p1Layout = new javax.swing.GroupLayout(p1);
        p1.setLayout(p1Layout);
        p1Layout.setHorizontalGroup(
            p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 384, Short.MAX_VALUE)
        );
        p1Layout.setVerticalGroup(
            p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 453, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(p1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(p1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel p1;
    private javax.swing.JTree tree;
    // End of variables declaration//GEN-END:variables

    @Override
    public void valueChanged(TreeSelectionEvent e) {
                TreePath num = e.getPath();
                int i = num.getPathCount();
                if(i==1)
                {
                    root.removeAllChildren();
                    loadRoot();
                    modelTree.setRoot(root);
                }
                if(i==2)
                {
                DefaultMutableTreeNode key = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                //System.out.println(key.toString());
                String st = key.toString();
                p1.removeAll();
                UpdateNV b = new UpdateNV(st);
                b.setSize(445, 430);
                b.setLocation(0, 0);
                b.setVisible(true);
                p1.add(b);
                p1.updateUI();
                p1.setVisible(true);
                }
                if(i==3)
                {
                    
                }
    }
    public void loadRoot()
    {
        try {
            Ketnoi a = new Ketnoi();
             Connection con = a.connect();
//           // root = new DefaultMutableTreeNode("Quản Lý Hàng Hóa");
//             model = new   DefaultTreeModel(root) ;
//             DefaultTreeModel modelTree = (DefaultTreeModel)tree.getModel();
//            modelTree.setRoot(root);

    PreparedStatement pst = con.prepareCall("Select * from bophan");
    ResultSet rs = pst.executeQuery();
    String str = null;
    String ten = null;
    while (rs.next())
{
    try {
        str = rs.getString(2);
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(str);
        ten =rs.getString(1);
        PreparedStatement pst1 = con.prepareCall("Select * from nhanvien where mabp = ? ");
        pst1.setString(1, ten);     
        ResultSet rs1 = pst1.executeQuery();
        String str1 = null;
        while(rs1.next())
        {
         str1=rs1.getString(2);
         DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(str1);
         System.out.println(node1.toString());
         node.add(node1);
            
        }
       // System.out.println("alo");
        root.add(node);
        
    } catch (SQLException ex) {
        Logger.getLogger(QuanLyHangHoa.class.getName()).log(Level.SEVERE, null, ex);
    }
}       } catch (SQLException ex) {
            Logger.getLogger(QuanLyHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
